<template>
    <div class="templ">
        <div class="checkCode">
            <div class="title">
                <h4>选号区</h4>
            </div>
            <div class="codes">
                <div class="blueBalls">
                    <div class="item">百位号码</div>
                    <div class="ballsBox">
                        <ul>
                            <li v-for="(bluecode,index) in blueList" @click="active100(index)"
                                :class="{active:isactive100 === index}">{{bluecode}}
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="blueBalls">
                    <div class="item">十位号码</div>
                    <div class="ballsBox">
                        <ul>
                            <li v-for="(bluecode,index) in blueList" @click="active10(index)"
                                :class="{active:isactive10 === index}">{{bluecode}}
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="blueBalls">
                    <div class="item">个位号码</div>
                    <div class="ballsBox">
                        <ul>
                            <li v-for="(bluecode,index) in blueList" @click="active1(index)"
                                :class="{active:isactive1 === index}">{{bluecode}}
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="btns">
                <mt-button type="danger" size="small" @click='randomOne'>机选一注</mt-button>
            </div>
            <div class="confirm">
                <mt-button type="default" disabled size="large" id="confirmBtn" @click="saveCode">确认选号</mt-button>
            </div>
            <div class="info">
                <p>您当前投注了{{buyCount}}注彩票，共{{2*buyCount}}元</p>
                <div class="content">
                    <div class="infoBox">
                        <ul>
                            <li v-for="(balls,index) in checkedBalls.data" @click="pick(index)">
                                <span v-for="ball in balls" v-if="!(isUpdate === index)">{{ball}}</span>
                                <input type="number" v-for="ball in balls" :value="ball" class="updateBox" v-if="isUpdate === index">
                                <input type="button" value="完成" class="updateBtn" @click="saveUpdate" v-if="isUpdate === index">
                            </li>
                        </ul>
                    </div>
                    <div class="option">
                        <input type="button" value="删除" @click="deleteCode">
                        <input type="button" value="修改" @click="updateCode">
                        <input type="button" value="清空" @click="emptyCodes">
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui';
    import {randomNum,deleteCode,updateCode,emptyCodes,clearConfirm,pick,comfirmed} from '../../kits/common'
    export default {
        data(){
            return {
                blueList: [],
                isactive100: -1,//表示百位是否为选中状态
                isactive10: -1,//表示十位是否为选中状态
                isactive1: -1,//表示个位是否为选中状态
                checkedBalls: {type: 'fc3d', data: []},
                buyCount: 0,//已选中的彩票数量
                picked: -1,//信息栏选中号码的下标
                isUpdate: -1
            }
        },
        created() {
            this.init();
        },
        methods: {
            init() {
                let arr = [];
                for (var i = 0; i <= 9; i++) {
                    arr[arr.length] = i;
                }
                this.blueList = arr;
            },
            active100(index) {
                this.isactive100 = index;
                this.confirm();
            },
            active10(index) {
                this.isactive10 = index;
                this.confirm();
            },
            active1(index) {
                this.isactive1 = index;
                this.confirm();
            },
            randomBall() {//随机生成3个数
                var arr = [];
                for (let i = 0; i < 3; i++) {
                    arr.push(randomNum(9, 1)[0]);
                }
                return arr;
            },
            clearActive() {//重置所有选择状态
                this.isactive100 = -1;
                this.isactive10 = -1;
                this.isactive1 = -1;
            },
            randomOne(){
                this.clearActive();
                let randomArr = this.randomBall();
                this.isactive100 = randomArr[0];
                this.isactive10 = randomArr[1];
                this.isactive1 = randomArr[2];
                this.confirm();
            },
            confirm() {
                //当百位十位个位都被选中后，comfirm按钮变为可点击
                if (this.isactive100 !== -1 && this.isactive10 !== -1 && this.isactive1 !== -1) {
                    comfirmed();
                }
            },
            saveCode() {
                //1.保存选中的号码到信息区域
                //将新生成的对象存到checkedBalls对象中
                this.checkedBalls.data.push([this.isactive100,this.isactive10,this.isactive1]);

                //2.清空现有的选中状态
                this.isactive100 = -1;
                this.isactive10 = -1;
                this.isactive1 = -1;
                clearConfirm();//清除按钮的提交状态防止无用提交

                //3.将选中的彩票数加1
                this.buyCount++;

            },
            pick(index) {//设置选号栏选项状态
                this.picked = pick(this.picked,index);
            },
            deleteCode() {//删除信息栏选中的号码
                this.buyCount = deleteCode(this.checkedBalls.data,this.picked,this.buyCount);
            },
            updateCode(){//修改选中的号码
                this.isUpdate = updateCode(this.picked,this.isUpdate);
            },
            saveUpdate() {//恢复原样保存修改的内容
                let ballsChange = document.querySelectorAll('.updateBox');
                let ballsNew = [];
                for (let i = 0; i < ballsChange.length; i++) {
                    let ball = ballsChange[i].value;
                    if(ball > 0 && ball < 10) {
                        ballsNew.push(+ball);//将字符串转为数字
                    }else {
                        Toast('选号不符合选号规则');
                        return;
                    }
                }
                this.checkedBalls.data[this.picked] = ballsNew;//将修改好的值赋值给原来的数组
                this.isUpdate = -1;//隐藏输入框

            },
            emptyCodes(){//清空选号列表
                this.buyCount = emptyCodes(this.checkedBalls,this.buyCount);
            }
        }
    }
</script>

<style scoped>
    .checkCode {
        padding: 0 10px;
    }

    /*标题区*/
    .checkCode .title {
        text-align: center;
        border-bottom: 1px solid #a1a1a1;
    }

    .title h4 {
        line-height: 30px;
    }

    .blueBalls {
        border-bottom: 1px solid #e1e1e1;
        display: flex;
        justify-content: space-between;
    }

    .item {
        width: 10%;
        padding: 10px;
        font-weight: 600;
    }

    /*选号区块*/
    .ballsBox {
        width: 90%;
    }

    .ballsBox ul {
        display: flex;
        padding: 0 60px;
        flex-wrap: wrap;
        justify-content: space-around;
    }

    .ballsBox ul li {
        width: 30px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        margin: 2px;
        border: 1px solid #e1e1e1;
        border-radius: 50%;
        color: #333;
        font-weight: 600;
        background-color: #f1f1f1;
        cursor: pointer;
    }

    .blueBalls .item {
        color: gold;
    }

    .blueBalls ul li.active {
        color: #fff;
        background-color: gold;
    }

    /*按钮区块*/
    .btns {
        display: flex;
        justify-content: space-around;
        padding: 10px 0;
    }

    .confirm {
        padding: 0 40px;
    }

    /*投注信息*/
    .info {
        margin-top: 10px;
    }

    .info p[data-v-7a3a87f4] {
        color: #333;
    }

    .info .content {
        display: flex;
        justify-content: space-between;
    }

    .info .infoBox {
        width: 80%;
        height: 120px;
        border: 1px solid #a1a1a1;
        overflow: scroll;
    }

    .info .infoBox ul {
        padding: 10px;
        margin: 0;
    }

    .info .infoBox li span {
        display: inline-block;
        width: 30px;
        text-align: center;
        padding: 2px;
        color: gold;
    }

    .info .option {
        width: 20%;
        height: 120px;
        padding-left: 10px;
    }

    .info .option input {
        margin-bottom: 2px;
        font-size: 14px;
        line-height: 18px;
        padding: 4px 6px;
        margin: 0;
    }

    .info .option input:nth-child(2) {
        margin: 15px 0;
    }

    /*当点击修改后出现的样式*/
    .infoBox [class^="updateBox"] {
        width: 20px;
        height: 28px;
        text-align: center;
        padding: 0;
        margin: 0;
        border: none;
        margin: 0 5px;
        background-color: #a1a1a1;
    }

    .infoBox{
        color: gold;
    }

    .infoBox .updateBtn {
        padding: 0;
        height: 28px;
        background-color: #e1e1e1;
    }
</style>